<template>
	<view class="">
		<!-- #ifdef APP-PLUS -->
		<!-- <e-popup bgColor="transparent" :show="versionModalShow" mode="center" @close="versionModalShow = false">
			<view class="app-component-upgrade">
				<view class="app-component-upgrade__fly e-rela e-flex-xy-center">
					<app-image :size="['calc(100% - 48rpx)','330rpx']" src="app_upgrade_bg.png" />
				</view>
				<view class="app-component-upgrade__content bg-white e-rela">
					<view class="e-flex">
						<text class="e-font-40 c-4d e-m-l-50">{{ $t('setting.发现新版本') }}</text>
						<view class="version-box e-flex-xy-center">
							<text class="c-white e-font-24">V{{ newVersion }}</text>
						</view>
					</view>
					<view class="upgrade-content e-m-l-50 e-m-r-50 e-m-t-40">
						<view class="e-flex e-flex-items-start">
							<view class="red-dot e-m-r-22 e-m-t-10"></view>
							<text>{{ versionText }}</text>
						</view>
					</view>
					<view class="upgrade-btn e-flex-xy-center" @tap="upgrade">
						<text class="c-white e-font-30">{{ $t('setting.立即升级') }}</text>
					</view>
				</view>
			</view>
		</e-popup> -->
		<!-- #endif -->
	</view>
</template>
<script lang="ts">
	import {
		defineComponent
	} from 'vue'
	export default defineComponent({
		name: 'upgrade',
	})
</script>
<script setup lang="ts">
	import { ref } from 'vue'
	// #ifdef APP
	import { getAppVersion } from '@/common/request/api'
	import { toastApp } from '@/common/utils/toast'
	// #endif

	import {
		useCurrentInstance
	} from '@/common/hooks'

	const props = defineProps({
		isAuto: Boolean
	})
	const emit = defineEmits(['showAppVersionTips'])

	const ctx = useCurrentInstance()

	// #ifdef APP
	const versionModalShow = ref(false)

	let nowVersion = ''
	const newVersion = ref('')
	const versionText = ref('')
	// 更新类型
	// 1 热更新
	// 2 整包更新
	let upgradeType = 1
	let downloadUrl = ''


	// 版本更新
	const checkVersion = (isAuto?: boolean, showToast ?: boolean) => {
		getAppVersion().then(res => {

			versionText.value = res.data.appVersionMsg
			let resversion = res.data.appVersion
			newVersion.value = resversion
			if (nowVersion != resversion) {
				let _nowVersion = nowVersion.split('.')
				let version = resversion.split('.')

				if (_nowVersion[0] != version[0] || _nowVersion[1] != version[1]) {
					if (_nowVersion[0] != version[0] && Number(_nowVersion[0]) - 0 > Number(version[0]) - 0) {
						showToast && toast()
						return
					}
					if (_nowVersion[1] != version[1] && Number(_nowVersion[1]) - 0 > Number(version[1]) - 0) {
						showToast && toast()
						return
					}
					upgradeType = 2
					if (uni.getSystemInfoSync().platform == 'ios') {
						downloadUrl = res.data.appIosUrl
					} else {
						downloadUrl = res.data.appAndroidUrl
					}

				} else {
					if (_nowVersion[2] != version[2] && Number(_nowVersion[2]) - 0 > Number(version[2]) - 0) {
						showToast && toast()
						return
					}
					upgradeType = 1
					downloadUrl = res.data.hotUpdateUrl
				}
				if (isAuto) {
					versionModalShow.value = true
				} else {
					emit('showAppVersionTips')
				}

			} else {
				showToast && toast()
			}

		})
	}
	const toast = () => {
		toastApp(ctx.$t('setting.versionIsNew'))
	}
	const upgrade = () => {
		// 下载app
		if (upgradeType == 1) {
			// 热更新
			plus.nativeUI.showWaiting(ctx.$t('setting.upgradeTxt'))

			uni.downloadFile({
				url: downloadUrl,
				success: downloadResult => {
					versionModalShow.value = false
					// console.log(downloadResult)
					if (downloadResult.statusCode === 200) {
						plus.runtime.install(
							downloadResult.tempFilePath,
							{
								force: false
							},
							function () {
								console.log('install success...')
								// uni.hideLoading()
								plus.nativeUI.closeWaiting()
								ctx.$showModal({
									content: ctx.$t('setting.upgradeSuccess'),
									showCancel: false,
									cancelText: '',
									complete: () => {
										plus.runtime.restart()
									}
								})
							},
							function (e) {
								console.error(e)
								// uni.hideLoading()
								plus.nativeUI.closeWaiting()
								ctx.$toast(ctx.$t('setting.upgradeError'))

							}
						)
					}
				},
				fail: err => {
					console.log(err)
					plus.nativeUI.closeWaiting()
				}
			})
		} else {
			versionModalShow.value = false
			plus.runtime.openURL(downloadUrl)
		}
	}


	plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
		// widgetInfo.version对应的是应用版本名称
		// widgetInfo.versionCode对应的是应用版本号
		nowVersion = widgetInfo.version
		checkVersion(props.isAuto)
	})

	defineExpose({
		checkVersion
	})

	// #endif
</script>

<style lang="scss" scoped>
	@include appBem_b(component-upgrade) {

		width: calc(100vw - 160rpx);

		@include appBem_e(fly) {
			z-index: 1;
		}

		@include appBem_e(content) {
			padding: 182rpx 0 50rpx 0;
			top: -182rpx;
			border-radius: 40rpx;
		}

		.version-box {
			height: 36rpx;
			border-radius: 20rpx;
			padding: 0 10rpx;
			margin-left: 10rpx;
			background: var(--color-primary);
		}

		.upgrade-btn {
			height: 90rpx;
			background: var(--color-primary);
			margin: 50rpx 50rpx 0 50rpx;
			border-radius: 45rpx;
		}

		.upgrade-content {
			max-height: 600rpx;
			overflow: auto;
		}

		.red-dot {
			height: 12rpx;
			width: 12rpx;
			background: $base-color-red;
			border-radius: 50%;
		}

	}
</style>